package com.example.viewtest.ActivityGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.viewtest.FragmentGroup.FragmentOne;
import com.example.viewtest.FragmentGroup.FragmentThree;
import com.example.viewtest.FragmentGroup.FragmentTwo;
import com.example.viewtest.R;
import com.example.viewtest.Tools.BaseActivity;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.BezierPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

public class MagicIndictaorActivity extends BaseActivity {
    private MagicIndicator magicIndicator;
    private ViewPager viewPager;
    private String title[] = new String[]{"FragmentOne", "FragmentTwo", "FragmentThree"};
    private Long sellerId;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magic_indictaor);
        magicIndicator = this.findViewById(R.id.magicindicator);
        viewPager = findViewById(R.id.viewpager);
        myAdapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(myAdapter);
        initMagicindicator();


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {

                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });

    }

    private void initMagicindicator() {

        magicIndicator.setBackgroundColor(Color.WHITE);
        //新建导航栏
        CommonNavigator commonNavigator = new CommonNavigator(this);
//        commonNavigator.setFollowTouch(true);
        //令头部View平分宽度，不设置AdjustMode为true则weight失效
        commonNavigator.setAdjustMode(true);
//        commonNavigator.setScrollPivotX(0.5f);
        commonNavigator.setEnablePivotScroll(true);
        commonNavigator.setRightPadding(50);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return title == null ? 0 : title.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
                commonPagerTitleView.setContentView(R.layout.simple_pager_title_layout);
                final TextView textView = commonPagerTitleView.findViewById(R.id.textview);
                final ImageView imageView = commonPagerTitleView.findViewById(R.id.im_view);
                textView.setText(title[index]);
                imageView.setImageResource(R.drawable.icon1);
                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
                    @Override
                    public void onSelected(int index, int totalCount) {
                        textView.setTextColor(Color.RED);
                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {
                        textView.setTextColor(Color.BLACK);
                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
                        imageView.setScaleX(1.3f + (0.8f - 1.3f) * leavePercent);
                        imageView.setScaleY(1.3f + (0.8f - 1.3f) * leavePercent);
                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
                        imageView.setScaleX(0.8f + (1.3f - 0.8f) * enterPercent);
                        imageView.setScaleY(0.8f + (1.3f - 0.8f) * enterPercent);
                    }
                });
                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                //设置标题指示器，也有多种,可不选，即没有标题指示器。
//                BezierPagerIndicator indicator = new BezierPagerIndicator(context);
//                indicator.setColors(Color.parseColor("#ff4a42"), Color.parseColor("#fcde64"), Color.parseColor("#73e8f4")
//                        , Color.parseColor("#76b0ff"), Color.parseColor("#c683fe")
//                        , Color.parseColor("#76b0ff"), Color.parseColor("#c683fe")
//                        , Color.parseColor("#76b0ff"), Color.parseColor("#c683fe"));
                return null;
            }

        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    }

    private class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    FragmentOne fragmentOne = new FragmentOne();
                    fragment = fragmentOne;
                    break;
                case 1:
                    FragmentTwo fragmentTwo = new FragmentTwo();
                    fragment = fragmentTwo;
                    break;
                case 2:
                    FragmentThree fragmentThree = new FragmentThree();
                    fragment = fragmentThree;
                    break;
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

    }
}
